package cn.tt.tuantuanerp.tuantuanerp.orderwebapi.controller;


import cn.tt.tuantuanerp.tuantuanerp.comment.pojo.dto.OrderAddNewDTO;
import cn.tt.tuantuanerp.tuantuanerp.comment.pojo.vo.OrderDetailsVO;
import cn.tt.tuantuanerp.tuantuanerp.comment.pojo.vo.OrderListItemVO;
import cn.tt.tuantuanerp.tuantuanerp.comment.security.LoginPrincipal;
import cn.tt.tuantuanerp.tuantuanerp.comment.utils.IdGenerator;
import cn.tt.tuantuanerp.tuantuanerp.comment.web.JsonPage;
import cn.tt.tuantuanerp.tuantuanerp.comment.web.JsonResult;
import cn.tt.tuantuanerp.tuantuanerp.orderwebapi.service.impl.OrderServiceImpl;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * 订单的控制器
 * @author Wangyujie
 */
@Api(tags = "4.订单模块")
@RestController
@Validated
@Slf4j
@RequestMapping("/order")
public class OrderController {

    @Autowired
    OrderServiceImpl orderService;

    @ApiOperation("生成订单")
    @ApiOperationSupport(order = 110)
    @PostMapping("/add-new")
    @PreAuthorize("hasAuthority('sale_staff')")
    public JsonResult<Void> addNew(@RequestBody OrderAddNewDTO orderAddNewDTO, @ApiIgnore @AuthenticationPrincipal LoginPrincipal loginPrincipal) {
        log.debug("开始处理【生成订单】的请求传入参数：{}",orderAddNewDTO);
        orderService.addNew(orderAddNewDTO,loginPrincipal);
        return JsonResult.ok();
    }

    @ApiOperation("查询订单")
    @ApiOperationSupport(order = 410)
    @GetMapping("/list")
    @PreAuthorize("hasAuthority('sale_staff')")
    public JsonResult<JsonPage<OrderListItemVO>> listByCreatorId(
            @ApiIgnore @AuthenticationPrincipal LoginPrincipal  loginPrincipal,Integer startIndex, Integer pageSize){
        log.debug("开始处理【查询订单】请求,用户id:{}",loginPrincipal.getId());
        return JsonResult.ok(orderService.listByCreatorId(loginPrincipal,startIndex,pageSize));
    }

    @ApiOperation("查询订单详情")
    @ApiOperationSupport(order = 420)
    @GetMapping("/list/detail")
    @PreAuthorize("hasAuthority('sale_staff')")
    public JsonResult<OrderDetailsVO> getDetailByOrderId(String id){
        log.debug("开始处理[查询订单详情]请求,订单id:{}",id);
        return JsonResult.ok(orderService.getDeatil(id));
    }

    @ApiOperation("订单号生成")
    @ApiOperationSupport(order = 440)
    @GetMapping("/get/id")
    @PreAuthorize("hasAuthority('sale_staff')")
    public JsonResult<String> getOrderId(@ApiIgnore @AuthenticationPrincipal LoginPrincipal loginPrincipal){
        log.debug("当事人:{}",loginPrincipal);
        String id = IdGenerator.getOrderId(loginPrincipal);
        return JsonResult.ok(id);
    }

    @ApiOperation("模糊查询订单")
    @ApiOperationSupport(order = 450)
    @GetMapping("/listByOrderId")
    @ApiImplicitParam(value = "订单号",name = "id",example = "2022102201")
    @PreAuthorize("hasAuthority('sale_leader_special')")
    public JsonResult<JsonPage<OrderListItemVO>> getOrdersByOrderId(@ApiIgnore @AuthenticationPrincipal LoginPrincipal loginPrincipal, String id, Integer startIndex, Integer pageSize){
        log.debug("正在模糊查询订单：{}",id);
        return JsonResult.ok(orderService.getOrdersByOrderId(loginPrincipal,id,startIndex,pageSize));
    }


    @ApiOperation("根据销售员ID查询订单")
    @ApiOperationSupport(order = 460)
    @GetMapping("/listByCreator")
    @ApiImplicitParam(value = "创建人ID",name = "id",example = "26")
    @PreAuthorize("hasAuthority('sale_staff')")
    public JsonResult<JsonPage<OrderListItemVO>> getOrdersByCreator(@ApiIgnore @AuthenticationPrincipal LoginPrincipal loginPrincipal,Long id,Integer startIndex,Integer pageSize) {
        log.debug("正在查询{}的订单",id);
        return JsonResult.ok(orderService.listByCreator(loginPrincipal,id,startIndex,pageSize));
    }

}
